<template>
  <a-modal
    ok-text="确认"
    cancel-text="取消"
    :maskClosable="false"
    centered
    :title="title"
    :visible="visible"
    :destroyOnClose="true"
    footer=""
    @cancel="handCancel"
  >
    <div id="meQrcode" :title="label">
      <div class="Shelves">{{ model.Shelves }}</div>
      <vue-qr
        :text="text"
        :size="200"
        :margin="0"
        colorDark="black"
        colorLight="white"
      ></vue-qr>
      <div class="labelBox" v-for="(item, index) in list" :key="index">
        {{ model.WareHouse }}
      </div>
    </div>
    <div class="modal-footer">
      <a-button
        :loading="loading"
        type="primary"
        @click="savePoster('meQrcode', title)"
        >{{ loading ? "正在下载中" : "下载" }}</a-button
      >
      <a-button @click="handCancel">取消</a-button>
    </div>
  </a-modal>
</template>
<script>
import VueQr from "vue-qr";
import { savePoster } from "@/unit/tool";
import Canvas2Image from "@/unit/canvas2Image.js";
import html2canvas from "html2canvas";
export default {
  props: {
    //二维码底部文字
    label: {
      default: "",
    },
    //logo尺寸（正方形 长宽相同）
    text: {
      default: "",
    },
  },
  components: { VueQr },
  data() {
    return {
      title: "",
      visible: false,
      list: [],
      model: {},
      loading: false,
    };
  },
  methods: {
    savePoster,
    showModal() {
      this.visible = true;
    },
    handCancel() {
      this.visible = false;
    },
    savePoster(id, downloadName) {
      let that = this;
      //打开loading
      that.loading = true;
      //生成图片之前，需先将整体的滚动条隐藏掉，否则生成出来的照片会偏移
      document.getElementById("app").style.overflowY = "hidden";
      //两个参数：所需要截图的元素id，截图后要执行的函数， canvas为截图后返回的最后一个canvas
      var shareContent = document.getElementById(id);
      var width = shareContent.offsetWidth;
      var height = shareContent.offsetHeight;
      var canvas = document.createElement("canvas");
      var scale = 5; //这里为了下载出更清晰的图片，值越大越清晰

      canvas.width = width * scale;
      canvas.height = height * scale;
      canvas.getContext("2d").scale(scale, scale);

      var opts = {
        scale: 1, //不管上面的scale是多少倍，这里只能取1，否则会显示不完全
        canvas: canvas,
        logging: true,
        width: width,
        height: height,
      };
      html2canvas(shareContent, opts).then(function(canvas) {
        //这个方式是直接将canvas保存为图片的，具体参数可自行查看源码
        var img = Canvas2Image.saveAsImage(
          canvas,
          canvas.width,
          canvas.height,
          "PNG",
          downloadName
        );
        //这个方式只是提供展示图片的功能，暂时没有用到
        // var img = Canvas2Image.convertToPNG(canvas, canvas.width, canvas.height,'PNG');
        //  document.body.appendChild(img);

        //生成图片之前，需先将整体的滚动恢复为之前的状态
        document.getElementById("app").style.overflowY = "scroll";
        //关闭loading
        that.loading = false;
      });
    },
  },
};
</script>
<style scoped lang="less">
.qrcode_box,
#meQrcode {
  width: 390px;
  height: 300px;
  display: inline-block;
  padding: 0px 10px 10px 10px;
}

.qrcode_box img {
  display: none;
}

.modal-footer {
  text-align: center;
  margin: 20px 0 0 0;
  button {
    margin: 0 20px;
  }
}
::v-deep .ant-modal-body {
  text-align: center !important;
}
.labelBox {
  text-align: center;
  font-size: 24px;
  margin: 5px 0;
}
.Shelves {
  text-align: center;
  font-size: 28px;
  font-weight: bold;
  margin: 2px 0 5px 0;
}
</style>
